package cloudContact.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Calendar;
import java.util.UUID;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.mybeans.factory.RollbackException;

import cloudContact.Common;
import cloudContact.Emailer;
import cloudContact.MyDAOException;
import cloudContact.DAO.*;

/**
 * Servlet implementation class Activate
 */
@WebServlet("/Activate")
public class Activate extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public Activate() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/plain");
		PrintWriter out = response.getWriter();

		try {
			System.out.println("Start Activate...");

			// Either user name or id must be entered.
			if (((request.getParameter("userName") == null) || request
					.getParameter("userName").toString().isEmpty())
					&& ((request.getParameter("id") == null) || request
							.getParameter("id").toString().isEmpty())) {
				out.print("Error: Invalid user name.");
				return;
			}

			if ((request.getParameter("code") == null)
					|| request.getParameter("code").toString().isEmpty()) {
				out.print("Error: Invalid activation code");
				return;
			}

			UnactivatedUserDAO unActDAO = UnactivatedUserDAO.getInstance();
			UserDAO userDAO = UserDAO.getInstance();

			UnactivatedUser user = null;

			String userName = null;
			int id;
			
			if (!(request.getParameter("userName") == null)
					&& !(request.getParameter("userName").toString().isEmpty())) {
				
				userName = request.getParameter("userName").toString();

				System.out.println("User name:" + userName);
				
				user = unActDAO.lookup(userName);

				if (user == null) {
					out.print("Error: The user is not yet registered.");
					return;
				}				
			} else {			
				id = Integer.parseInt(request.getParameter("id").toString());

				System.out.println("ID:" + id);
				
				user = unActDAO.lookup(id);

				if (user == null) {
					out.print("Error: The user is not yet registered.");
					return;
				}
			}

			String code = request.getParameter("code").toString().trim();
			if (!user.getActivationCode().equals(code)) {
				out.print("Error: Incorrect activation code");
				return;
			}

			User newUser = new User();

			newUser.setPassword(user.getPassword());

			newUser.setEmail(user.getEMail());

			newUser.setCreatedDate(user.getCreatedDate());

			userDAO.create(newUser);

			unActDAO.delete(user.getUserId());

			out.print("Success:");

		} catch (RollbackException e) {
			out.print(Common.CriticalErrorRaised(e));
		} catch (MyDAOException e) {
			out.print(Common.CriticalErrorRaised(e));
		} catch (Exception e) {
			out.print(Common.CriticalErrorRaised(e));
		} finally {
			out.flush();
			out.close();
		}
	}

}
